package com.ren.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SysConfig {
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
    //创建一个队列，队列名字为queueOne001
    @Bean
    public Queue queueOne(){
        return new Queue("queueOne001");
    }
    @Bean
    public Queue queue002(){
        return new Queue("queueOne002");
    }
    @Bean
    public Queue queue003(){
        return new Queue("queueOne003");
    }
    @Bean
    public Queue queue004(){
        return new Queue("queueOne004");
    }
    @Bean
    public Queue queue005(){
        return new Queue("queueOne005");
    }
    //默认交换机
   @Bean
   public DirectExchange directExchange(){
        return new DirectExchange("directExchange001");
    }
    //主题交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange001");
    }
    //扇形交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange001");
    }
    @Bean
    public Binding queueOneToDirectExchange(Queue queueOne, DirectExchange directExchange){
     return    BindingBuilder.bind(queueOne).to(directExchange).with("movie.buy");
    }
    @Bean
    public Binding queue002ToTopicExchange(Queue queue002, TopicExchange topicExchange){
        return    BindingBuilder.bind(queue002).to(topicExchange).with("buy.#");
    }
    @Bean
    public Binding queue003ToTopicExchange(Queue queue003, TopicExchange topicExchange){
        return    BindingBuilder.bind(queue003).to(topicExchange).with("out.#");
    }
    @Bean
    public Binding queue004ToFanoutExchange(Queue queue004, FanoutExchange fanoutExchange){
        return    BindingBuilder.bind(queue004).to(fanoutExchange);
    }
    @Bean
    public Binding queue005ToFanoutExchange(Queue queue005, FanoutExchange fanoutExchange){
        return    BindingBuilder.bind(queue005).to(fanoutExchange);
    }
}